<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */
namespace app\admin\controller;
use app\admin\logic\GoodsLogic;
use think\AjaxPage;
use think\Page;
use think\Db;

header("Content-Type: text/html; charset=utf8");
class Discuss extends Base {
	
	/*
    * 初始化操作
    */
    public function _initialize()
    {
        parent::_initialize();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }
    }    

    public function index(){
    
        return $this->fetch();
    }
    
    /*
     * 帖子管理
     * */
    public function noteManage(){
    	$lists = M('discuss')->order("addtime desc")->select();
    	if($lists){
    		foreach($lists as $key=>$val){
    			$suppliers = M('suppliers')->where('suppliers_id',$val['suppliers_id'])->field('suppliers_id,suppliers_name')->find();
    			$lists[$key]['suppliers_name'] = $suppliers['suppliers_name'];
    			$lists[$key]['reply_num'] = M('discuss_reply')->where('discuss_id',$val['discuss_id'])->count();
    		}
    	}
    	$status = array('待审核','通过','已关闭');
    	$this->assign('status',$status);
    	$this->assign('lists',$lists);
    	return $this->fetch();
    }
    
    /*
     * 发布修改帖子
     * */
    public function addeditNote(){
    	$discuss_id = empty(I('discuss_id'))?0:I('discuss_id');
    	$discuss = M('discuss')->where('discuss_id',$discuss_id)->find();
    	if($discuss){
    		$discuss['img'] = json_decode($discuss['img'],true);
    	}
   
    	if(IS_POST){
    		$data = I('post.');
    	   if(empty(I('post.title'))){
    		    $return_arr = array(
    		        'status' => -1,
    		        'msg' => '标题不能为空',
    		        'data' => '标题不能为空',
    		    );
    		    return  $this->ajaxReturn($return_arr);
    		}
    		$data['type'] = empty($data['type'])?'2':$data['type'];
    		$data['user_id'] = session('admin_id');//type不为0时，user_id都是当前管理员ID，请查询时区分判断一下
    		$data['img'] = json_encode(array_filter($data['img']));
    		$data['addtime'] = time();
    		if(empty($data['discuss_id'])){
    			$r = M('discuss')->add($data);
    		}else{
    			$r = M('discuss')->where('discuss_id',$data['discuss_id'])->save($data);
    		}
    		if($r){
				$return_arr = array(
	                'status' => 1,
	                'msg' => '操作成功',
	                'data' => array('url' => U('admin/Discuss/noteManage')),
	            );
		    	return  $this->ajaxReturn($return_arr); 
			}
			else{
				$return_arr = array(
	                'status' => -1,
	                'msg' => '操作失败',
	                'data' => '入库失败，请联系管理员',
	            );
		    	return  $this->ajaxReturn($return_arr); 
			}
    		
    	}
    	$suppliers = M("suppliers")->select();
    	$type = array('1'=>'酒厂帖','2'=>'平台帖');
    	$status = array('审核','开启','关闭');
    	$this->assign('type',$type);
    	$this->assign('status',$status);
    	$this->assign('suppliers',$suppliers);
    	$this->assign('discuss',$discuss);
    	return $this->fetch();
    }
    
    /*
     * 发布修改帖子
     * */
    public function delNote(){
    	$discuss_id = empty(I('discuss_id'))?0:I('discuss_id');
    	$r = M('discuss')->where('discuss_id',$discuss_id)->delete();
    	if($r){
			$return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('admin/Discuss/noteManage')),
            );
	    	return  $this->ajaxReturn($return_arr); 
		}
		else{
			$return_arr = array(
                'status' => -1,
                'msg' => '操作失败',
                'data' => '请联系管理员',
            );
	    	return  $this->ajaxReturn($return_arr); 
		}
    }
    
    
    /*
     * 发布修改帖子
     * */
    public function replyManage(){
        $discuss_id = empty(I('discuss_id'))?0:I('discuss_id');
        $lists = M('discuss_reply')->where(array('discuss_id'=>$discuss_id,'parent_id'=>0))->select();
        foreach($lists as $key=>$val){
        	$user = M('users')->where('user_id',$val['user_id'])->find();
        	$lists[$key]['user'] = $user;
        	$lists2 = M('discuss_reply')->where(array('discuss_id'=>$discuss_id,'parent_id'=>$val['id']))->select();
        	if(!empty($lists2)){
        	    foreach($lists2 as $k=>$v){
	        		$user2 = M('users')->where('user_id',$v['user_id'])->find();
	        		$lists2[$k]['user'] = $user2;
	        	}
        	    $lists[$key]['lists'] = $lists2;	
        	}
        }
        $status = array('待审核','通过');
        $this->assign('status',$status);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    
    
    /*
     * 发布修改帖子
     * */
    public function doReply(){
        $id = empty(I('id'))?0:I('id');
        $act = I('act');
        if($act == 'success'){
        	$r = M('discuss_reply')->where(array('id'=>$id))->save(array('status'=>1));        	
        }
        if($act == 'close'){
        	$r = M('discuss_reply')->where(array('id'=>$id))->save(array('status'=>0));   
        }
        if($act == 'del'){
            $r = M('discuss_reply')->where(array('id'=>$id))->delete();  	
        } 
        if($r){
			$return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('admin/Discuss/replyManage')),
            );
	    	return  $this->ajaxReturn($return_arr); 
		}
		else{
			$return_arr = array(
                'status' => -1,
                'msg' => '操作失败',
                'data' => '请联系管理员',
            );
	    	return  $this->ajaxReturn($return_arr); 
	    }	
    }	
}